<template>
  <div class="content-wrap">
    <el-row class="btnWrap">
      <el-button
        icon="el-icon-plus"
        class="btn left-btn"
        type="primary"
        @click="showInput"
      >
        新增
      </el-button>
    </el-row>

    <el-table fit border :data="tableList" height="500" stripe>
      <el-table-column
        label="id"
        align="center"
        width="50px"
        header-align="center"
        prop="tk_id"
      ></el-table-column>
      <el-table-column
        label="库名"
        align="center"
        header-align="center"
        prop="name"
      ></el-table-column>
      <el-table-column
        label="创建人"
        align="center"
        header-align="center"
        prop="owner"
        :formatter="TeacherFormatter"
      ></el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        header-align="center"
        prop="date"
      ></el-table-column>
      <el-table-column
        label="做题时间/分钟"
        align="center"
        header-align="center"
        prop="time"
      ></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="item">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            @click="editListItem(item.$index, item.row)"
          >
            编辑
          </el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="deleteListItem(item.$index)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增 -->
    <el-dialog title="新增" :visible.sync="isShowEditAlert">
      <el-form label-width="100px" :inline="true">
        <el-form-item label="库名" prop="name">
          <el-input v-model="form.name" placeholder=""></el-input>
        </el-form-item>
        <el-form-item label="做题时间" prop="time">
          <el-input v-model="form.time" placeholder="">
            <template slot="append">分钟</template>
          </el-input>
        </el-form-item>
        <el-form-item label="创建人" prop="owner">
          <el-select v-model="form.owner" placeholder="请选择">
            <el-option
              v-for="item in teac_list"
              :key="item.teac_id"
              :label="item.name"
              :value="item.teac_id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template slot="footer">
        <el-button type="success" @click="submitForm"> 提交 </el-button>
        <el-button type="success" @click="submitClose1"> 取消 </el-button>
      </template>
    </el-dialog>

    <el-dialog title="提示" width="30%" center :visible.sync="isShowEditAdd">
      <b>请确认是否提交！</b>
      <template slot="footer">
        <el-button type="success" @click="submitFormAdd"> 确认 </el-button>
        <el-button type="success" @click="submitClose2"> 取消 </el-button>
      </template>
    </el-dialog>

    <!-- 编辑 -->
    <el-dialog title="编辑" :visible.sync="isShowEditDialog">
      <el-form label-width="100px" :inline="false">
        <el-form-item label="库名" prop="name">
          <el-input v-model="formModify.name" placeholder=""></el-input>
        </el-form-item>
        <el-form-item label="做题时间" prop="time">
          <el-input v-model="formModify.time" placeholder="">
            <template slot="append">分钟</template>
          </el-input>
        </el-form-item>
        <el-form-item label="创建人" prop="owner">
          <el-select v-model="formModify.owner" placeholder="请选择">
            <el-option
              v-for="item in teac_list"
              :key="item.teac_id"
              :label="item.name"
              :value="item.teac_id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="修改题目">
          <el-button @click="goEditTs(formModify.tk_id)">编辑题库</el-button>
        </el-form-item>
      </el-form>
      <template slot="footer">
        <el-button type="success" @click="submitFormDialog"> 提交 </el-button>
        <el-button type="success" @click="submitClose1"> 取消 </el-button>
      </template>
    </el-dialog>

    <el-dialog title="提示" width="30%" center :visible.sync="isShowEditModify">
      <b>请确认是否修改！</b>
      <template slot="footer">
        <el-button type="success" @click="submitFormModify"> 确认 </el-button>
        <el-button type="success" @click="submitClose2"> 取消 </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { submitFormAdd } from "api/admin/TK";
import { deleteListItem } from "api/admin/TK";
import { submitFormModify } from "api/admin/TK";
import { GetAllStudent } from "api/admin/TK";

import { getTeacList } from "api/opt"

export default {
  name: "StudentInfo",
  data() {
    return {
      tableList: [],
      isShowDeleteAlert: false,
      //增加
      isShowEditAlert: false,
      isShowEditAdd: false,
      //修改
      isShowEditDialog: false,
      isShowEditModify: false,
      allDevList: [],
      form: {},
      formModify: {},
      teac_list: [],
    };
  },

  methods: {
    goEditTs(id) {
      this.$router.push("/admin/TK/" + id);
    },
    GetAllStudent() {
      GetAllStudent().then((res) => {
        this.tableList = res.data;
        this.isShowDeleteAlert = false;
        this.isShowEditAlert = false;
        this.isShowEditAdd = false;
        this.isShowEditDialog = false;
        this.isShowEditModify = false;
      });
    },
    deleteListItem(index) {
      let tk_id = this.tableList[index].tk_id;
      console.log(tk_id)
      deleteListItem(tk_id).then(res => {
        if (res.state === "success") {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.GetAllStudent();
        } else {
          this.$message({
            message: "删除失败:" + res.data.err,
            type: "error",
          });
        }
      });
    },
    submitFormAdd() {
      submitFormAdd(this.form.name, this.form.owner, this.form.time).then(
        (res) => {
          console.log(res);
          if (res.state === "success") {
            this.$message({
              message: "添加成功",
              type: "success",
            });
            this.GetAllStudent();
          } else {
            this.$message({
              message: "添加失败:" + res.err,
              type: "error",
            });
            this.isShowEditAdd = false;
          }
        }
      );
    },
    submitFormModify() {
      submitFormModify(
        this.formModify.name,
        this.formModify.time,
        this.formModify.tk_id,
        this.formModify.owner
      ).then((res) => {
        console.log(res);
        if (res.state === "success") {
          this.$message({
            message: "修改成功",
            type: "success",
          });
          this.GetAllStudent();
        } else {
          this.$message({
            message: "修改失败:" + res.err,
            type: "error",
          });
          this.isShowEditModify = false;
        }
      });
    },
    getTeacList() {
      getTeacList().then((res) => {
      this.teac_list = res.data;
    });
    },
    changePage(index) {
      let that = this;
      that.page.index = index;
      that.GetAllStudent();
    },
    //控制dialog出现消失
    showInput() {
      let that = this;
      that.isShowEditAlert = true;
    },
    submitForm() {
      let that = this;
      that.isShowEditAdd = true;
    },
    submitClose1() {
      let that = this;
      that.isShowEditAlert = false;
      that.isShowEditDialog = false;
    },
    submitClose2() {
      let that = this;
      that.isShowEditAdd = false;
      that.isShowEditModify = false;
    },
    TeacherFormatter(row) {
      const id = row.owner;
      let name = null;
      this.teac_list.forEach((item) => {
        if (item.teac_id == id) {
          name = item.name;
        }
      });
      return name || "null";
    },
    editListItem(index) {
      let that = this;
      that.isShowEditDialog = true;
      that.formModify = JSON.parse(JSON.stringify(that.tableList[index]));
      console.log(that.formModify);
    },
    submitFormDialog() {
      let that = this;
      that.isShowEditModify = true;
    },
    formatGender(row) {
      if (row.gender == "1") {
        return "男";
      } else if (row.gender == "0") {
        return "女";
      }
    },
  },
  mounted() {
    let that = this;
    that.getTeacList();
    that.GetAllStudent();
  },
};
</script>

<style scoped>
.content-wrap .btn {
  position: absolute;
  z-index: 100;
}
.content-wrap .btnWrap {
  position: relative;
  height: 50px;
}
.content-wrap .left-btn {
  left: 0;
}
.content-wrap .right-btn {
  right: 0;
}
</style>